package com.esd.crm.controller;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.kaptcha.Kaptcha;

/**
 * 验证码
 * @author snailzhang
 *
 */
@RestController
@RequestMapping("/kaptcha")
public class KaptchaController {

  @Autowired(required=true)
  private Kaptcha kaptcha;

  @GetMapping("/render")
  public void render(HttpSession session) {
    String s= kaptcha.render();
    session.setAttribute("checkCode", s);
  }

  @PostMapping("/valid")
  public void validDefaultTime(@RequestParam String code) {
    //default timeout 900 seconds
    kaptcha.validate(code);
  }

  @PostMapping("/validTime")
  public void validCustomTime(@RequestParam String code) {
    kaptcha.validate(code, 60);
  }

}